package com.xm.settlement.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.xm.settlement.commons.base.ApiSQLException;
import com.xm.settlement.mapper.AppScoreLogMapper;
import com.xm.settlement.model.AppRecharge;
import com.xm.settlement.model.AppScoreLog;
import com.xm.settlement.model.AppUserManage;
import com.xm.settlement.service.AppScoreLogService;
import com.xm.settlement.service.AppUserManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/9.
 */
@Service
@Configurable(value = "message.properties")
public class AppScoreLogServiceImpl extends SuperServiceImpl<AppScoreLogMapper,AppScoreLog> implements AppScoreLogService {

    @Autowired private  AppScoreLogMapper appScoreLogMapper;
    @Autowired private AppUserManageService appUserManageService;


    @Override
    public void insertScoreLogFail(Map map) {
        appScoreLogMapper.insertScoreLogFail(map);
    }

    @Override
    public void insertScoreLogSuccess(Map map) {
        appScoreLogMapper.insertScoreLogSuccess(map);
    }

    @Override
    public void insertBatchByList(List<AppRecharge> appRecharges) {
        appScoreLogMapper.insertBatchByList(appRecharges);
    }
    //1：现金积分:  2：电子积分:  3:期权积分、 4:购物积分
    String scoreArray[] = new String[]{"","现金积分","电子积分","期权积分","购物积分"};


    /**
     * 积分互转 规则 rule：#
     * #############################################################
     1：现金积分:  2：电子积分:  3:期权积分、 4:购物积分、
     A:现金积分:
     1：可以转换，电子积分，但是不可以逆转。
     2：可以转换购物积分，但是不可以逆转。
     #############################################
     B:电子积分：
     1：可以给上下级部门转账，
     2：可以兑换购物积分，
     #############################################
     C：购物积分
     1：购物积分，在商城消费使用。以及转换所获得购物积分
     2:会员之间可以互转购物积分。
     #############################################
     D：期权积分
     1：期权积分。在三年内转换为原始股票。
     2：是不可以互转的。
     #############################################################
     @desc 1：现金积分:  2：电子积分:  3:期权积分、 4:购物积分
     * */
    @Override
    public void insertScoreToScore(String vipCode, Integer beforeType, Integer afterType, double score) throws ApiSQLException{
        if( afterType < beforeType ){
            throw new ApiSQLException(4400,scoreArray[beforeType]+"类型不能强制转换为"+scoreArray[afterType]);
        }

        switch (beforeType){
            case  1:

                break;
            case 2:

                break;
            case 3:

                break;
            case 4:
                break;
            default:
                return;
        }
        //-- 查询检索积分操作
        AppUserManage appUserManage = new AppUserManage();
        appUserManage.setVip_code(vipCode);
        appUserManage =  appUserManageService.selectOne(appUserManage);
        if(appUserManage == null ){
            throw new ApiSQLException(106,"会员不存在");
        }

        AppScoreLog appScoreLog = new AppScoreLog();
        appScoreLog.setVip_code(vipCode);
        appScoreLog.setOld_score(beforeType);
        appScoreLog.setNew_score(afterType);
        appScoreLog.setType(1);
        appScoreLog.setNum_value(score);
        appScoreLog.setCreate_time(new Date());
        //-- 现金积分转电子积分
        if ( beforeType == 1 && afterType == 2){
            if(appUserManage.getCash_score()<score){
                throw new ApiSQLException(4401,"积分不足");
            }
            insert(appScoreLog);
            return ;
        }
        //-- 现金积分转购物积分
        if ( beforeType == 1 && afterType == 4){
            if(appUserManage.getCash_score()<score){
                throw new ApiSQLException(4401,"积分不足");
            }
            insert(appScoreLog);
            return ;
        }
        //电子转购物积分
        if( beforeType == 2 && afterType == 4){
            if(appUserManage.getElect_score()<score){
                throw new ApiSQLException(4401,"积分不足");
            }
            insert(appScoreLog);
            return ;
        }
        throw new ApiSQLException(4400,scoreArray[beforeType]+"类型不能强制转换为"+scoreArray[afterType]);

    }






}
